package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddreseController {
    @Reference
    private MemberService memberService;
    @Reference
    private UserService userService;
//    @Autowired
//    private JedisPool jedisPool;
    // 查询健康管理师
@RequestMapping("/findHealthManagerList")
public Result findHealthManagerList(){
    System.out.println("hello");
    try {
        List<Map<String,String>> list = userService.findHealthManagerList();
        return new Result(true,"查询健康管理师成功",list);
    }catch (Exception e){
        e.printStackTrace();
        return null;
    }
}
// 查询会员信息
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        System.out.println("hello");
        try {
            //  如果用了分组,再第二页会搜不到第一页中的数据
            return memberService.findPage(queryPageBean);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

// 添加会员信息
    @RequestMapping("/add")
    public Result add(@RequestBody Member member){
        System.out.println("hello");
        try {
            memberService.addMember(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }
    }
    // 编辑会员信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member){
        try {
            memberService.edit(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }
// 删除会员信息
    @RequestMapping("/deleteById/{id}")
    public Result deleteById( @PathVariable Integer id){
        System.out.println(id);
        try {
//            Integer id  = Integer.parseInt(ids);
            memberService.delete(id);
            return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }

    }


}
